package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.transferproduct.pojo.dto.TransferProductDeleteDTO;
import com.xbongbong.pro.transferproduct.pojo.dto.TransferProductListDTO;
import com.xbongbong.pro.transferproduct.pojo.vo.TransferProductDeleteVO;
import com.xbongbong.pro.transferproduct.pojo.vo.TransferProductListVO;
import com.xbongbong.saas.domain.entity.TransferProductEntity;
import com.xbongbong.saas.model.TransferProductModel;
import com.xbongbong.saas.model.TransferProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/2/28 14:10
 * @since v1.0
 */
@Service("transferProductService")
public class TransferProductServiceImpl implements TransferProductService {

    private static final Logger LOG = LoggerFactory.getLogger(TransferProductServiceImpl.class);

    @Resource
    private TransferProductModel transferProductModel;

    @Override
    public TransferProductListVO findTransferProductList(TransferProductListDTO transferProductListDTO) throws XbbException {
        TransferProductListVO transferProductListVO = new TransferProductListVO();
        try {
            Map<String, Object> param = BeanUtil.convertBean2Map(transferProductListDTO, true);
            List<TransferProductEntity> transferProductList = transferProductModel.findEntitys(param);
            if (Objects.isNull(transferProductList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            transferProductListVO.setTransferProductList(transferProductList);
        } catch (Exception e) {
            LOG.error("transferProductServiceImpl.findTransferProductList 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return transferProductListVO;
    }

    @Override
    public TransferProductDeleteVO deleteBatch(TransferProductDeleteDTO transferProductDeleteDTO) throws XbbException {
        TransferProductDeleteVO transferProductDeleteVO = new TransferProductDeleteVO();
        try {
            List<Long> idList = transferProductDeleteDTO.getIdList();
            String corpid = transferProductDeleteDTO.getCorpid();
            if (!idList.isEmpty()) {
                transferProductModel.deleteBatch(idList, corpid, DelEnum.DELETE.getDel());
            }
        } catch (Exception e) {
            LOG.error("transferProductServiceImpl.deleteBatch 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return transferProductDeleteVO;
    }
}
